package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertDataHandleDto;
import com.zzyl.dto.AlertDataPageQueryDto;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertDataVo;
import com.github.pagehelper.Page;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class AlertDataServiceImpl implements AlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Override
    public PageResponse<AlertDataVo> getAlertDataPage(AlertDataPageQueryDto alertDataPageQueryDto) {
        // 获取当前的用户
        String subject = UserThreadLocal.getSubject();
        UserVo userVo = JSONUtil.toBean(subject, UserVo.class);
        PageHelper.startPage(alertDataPageQueryDto.getPageNum(), alertDataPageQueryDto.getPageSize());
        //时间判断是否为空不为空转为LocalDateTime
        LocalDateTime endTime1=null;
        LocalDateTime startTime1=null;
        if(alertDataPageQueryDto.getEndTime()!=null){
             endTime1= LocalDateTimeUtil.of(alertDataPageQueryDto.getEndTime());
        }
        if(alertDataPageQueryDto.getStartTime()!=null){
            startTime1= LocalDateTimeUtil.of(alertDataPageQueryDto.getStartTime());
        }

        Page<AlertDataVo> page=alertDataMapper.getAlertDataPage(alertDataPageQueryDto.getId(), alertDataPageQueryDto.getStatus(), alertDataPageQueryDto.getDeviceName(),userVo.getId(),startTime1,endTime1);
        return PageResponse.of(page, AlertDataVo.class);



    }

    @Override
    public void handleAlertDate(Long id, AlertDataHandleDto alertDataHandleDto) {
        alertDataMapper.handleAlertDate(id, alertDataHandleDto);

    }
}
